#include<config.h>



	 
int g_ItemsCfgCnt = 0;
ItemCfg g_ItemsCfgBuf[ITEMCFG_MAX_NUM];


int ParseConfigFile(void)
{
	 FILE *fp;
	char buf[100];
	char* p = buf;
	
	//打开文件
	fp = fopen(CFG_FILE, "r");
	if(fp < 0)
	{
		printf("open %s err\n",CFG_FILE);
		return -1;
	}
	
	/* 解析文件 */
	
	/*fgets : Reading stops after an EOF or a newline.	
	If a newline is read, it is stored into the buffer. 
	A terminating null byte ('\0') is stored after the last character in the buffer.
	*/
	while(fgets(buf, 100, fp))
	{ 
		p = buf;
		
		//吃掉开头的空格或TAB 
		while(*p == ' '|| *p =='\t')
			p++;
		
		//skip # line 
		if(*p == '#')
		continue;
		
		//parse
		g_ItemsCfgBuf[g_ItemsCfgCnt].index = g_ItemsCfgCnt;
		sscanf(p, "%s %d %s",   g_ItemsCfgBuf[g_ItemsCfgCnt].name, 
								 &g_ItemsCfgBuf[g_ItemsCfgCnt].bCanBeTouched, 
								 g_ItemsCfgBuf[g_ItemsCfgCnt].command);
		g_ItemsCfgCnt++;
	}
	return 0;
}

int GetItemCfgCount(void)
{
	return g_ItemsCfgCnt;
}

PItemCfg GetItemCfgByIndex(int index)
{
	if (index <  g_ItemsCfgCnt)
		return &g_ItemsCfgBuf[index];
	else
		return NULL;
}

PItemCfg GetItemCfgByName(char *name)
{
	int i = 0;
	for (i = 0; i < g_ItemsCfgCnt; i++)
	{
		if (strcmp(name, g_ItemsCfgBuf[i].name) == 0)
			return &g_ItemsCfgBuf[i];
	}
	return NULL;
}


